<template>
<div class="direct-select">
    <!-- 选球区域 -->
    <div class="game-area">
        <div class="game-area-forward">
            <div class="game-area-forward-title">
                <div class="game-area-forward-title-text">
                    <text class="game-area-forward-title-tips">至少选</text>
                    <text class="game-area-forward-title-tips" style="color: #E70D0D; font-size:27px;margin-left:4px;margin-right:4px;">3</text>
                    <text class="game-area-forward-title-tips">个号，长按设胆（最多</text>
                    <text class="game-area-forward-title-tips" style="color: #E70D0D; font-size:27px;margin-left:4px;margin-right:4px;">2</text>
                    <text class="game-area-forward-title-tips">个胆码）</text>
                </div>
                <!-- @click="shakephone" -->
                <image class="game-area-forward-title-shake" src="http://zhongcai-test.oss-cn-beijing.aliyuncs.com/test-huicai-admin/shakep.png" @click="shake"></image>
            </div>
            <ball-list
                class="ball-list-demo"
                :defaultValue = defaultBall
                :defaultDanValue = defaultDanBall
                :ball-section="[0,9]"
                :ballLenth="5"
                :maxDanBall="2"
                ballType="BileCode"
                :errorCallbak="judgeFn"
                @castBallList="getBallList"
                @castDanBallList="getDanBallList"
            ></ball-list>
        </div>
    </div>
    <!-- 选球区域 -->
</div>
</template>
<script>
import ballList from "@/components/lotteries/components/ballList";
export default {
    data() {
        return {
           selectBall:[],
           selectDanBall:[],
           defaultBall:[],
           defaultDanBall:[]
        }
    },

    watch:{
        selectBall() {
            this.total()
        },

        selectDanBall() {
            this.total()
        }
    },


    components:{
        ballList
    },

    methods:{
        // 获取普通号的选值
        getBallList(value) {
            this.selectBall = value
        },

        // 获取胆码的选值
        getDanBallList(value) {
            this.selectDanBall = value
        },


        // 添加规则判断
        judgeFn(param) {
            let { type, DanBall} = param
            
            if( type === 'BileCode' && DanBall.length > 2) {
                modal.toast({ message: `胆码最多选择2个`}, 0.5)
                return false
            }

            return true
        },

        // 计算金额和注数
        total() {
            let total = {},
                len = this.selectBall.length
            if( !( len +  this.selectDanBall.length >= 3) ) {
                this.$emit('setTotal', total, [this.selectBall, this.selectDanBall])
                return false;
            }

            
            let num = 0,
                ballLen = this.selectBall.length,
                danBallLen = this.selectDanBall.length;

            
            if( danBallLen <= 0) {
                num = this.combination(ballLen, 3)
            } else {
                num = this.combination(ballLen, 3 - danBallLen)
            }

            total = {
                no      : this.selectBall,
                dan     : this.selectDanBall,
                numberType:4,
                type    : 'basisM',
                number  : num,
                money : num*2
            }

            this.$emit('setTotal', total, [this.selectBall, this.selectDanBall])
        },

        // 摇一摇随机一组
        shake() {
            this.defaultDanBall = []
            this.defaultBall = this.createRandowArr(0, 9, 3)
        },
        
        // 随机数值
        createRandowArr(min, max, length) {
            let r = [],
                o = {},
                a;

            for (var i = 0; i < length; i++) {
                a = Math.floor(min + max * Math.random())

                o[a] ? i-- : (r.push(a), (o[a] = true))
            }

            return r.sort((a, b) => (a < b ? -1 : 1))
        },

        
        
        // 随机指定几组数值
        randowFn(len) {
            let iarr = []
            for(var i = 0; i < len; i++) {
                iarr.push(this.createRandowArr(0, 9, 3))
            }
            
            return iarr
        },

        // 随机返回数值
        randowParam(len) {
            let iarr = this.randowFn(len),
                paramIarr = []
            
            iarr.forEach((v,i)=>{
                paramIarr.push({
                    "no": v,
                    "numberType": "4",
                    "type": "multiple",
                    "money": 2,
                    "number": 1
                })
            })

            return paramIarr
        },

        //组合函数
        combination(m, n) {
            //m为用户选了多少个，n为 **!规则!** 内一次取多少个数
            return this.factorial(m, n) / this.factorial(n, n);
        },

        //阶乘，
        factorial(m, n) {
            var num = 1, count = 0;
            
            for (var i = m; i > 0; i--) {
                if (count == n) {
                    break
                }

                num = num * i
                count++
            }

            return num
        }
    }
}
</script>
<style scoped>
/* game-area */
span {
  align-self: center;
  font-weight: bold;
}
.game-area-forward-title-text {
  flex-direction: row;
}
.game-area-forward-title {
	flex-direction: row;
	justify-content: space-between;
	height: 48px;
	display: flex;
	align-items: center;
	margin-bottom:32px;
}
.game-area-forward-title-shake {
    width: 44px;
    height: 44px;
}

.game-area-forward {
  padding-top: 30px;
  padding-bottom: 10px;
  padding-left: 32px;
  padding-right: 46px;
  width:750px;
}

.game-area-forward-title-tips {
	align-self: center;
	font-family: PingFangSC-Semibold;
	font-size: 24px;
	color: #333333;
}

/* game-area */
</style>

